#include "stdafx.h"
#include "jc.h"

JException::JException(const wchar_t *file, const wchar_t *func, int line, const wchar_t *message, ...)
	:m_file(file),m_func(func),m_line(line),m_message(NULL)
{
	JString text;
	va_list args;

	va_start(args, message);
	text.formatV(message, args);

	m_message = wcsdup(text);
}

JException::JException(const wchar_t *file, const wchar_t *func, int line, unsigned long code, const wchar_t *message, ...)
	:m_file(file),m_func(func),m_line(line),m_message(NULL)
{
	JString text, text2;
	int n;
	wchar_t buf[1000] = {0};
	va_list args;

	va_start(args, message);
	text.formatV(message, args);

	n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, buf, sizeof(buf)/sizeof(buf[0]), NULL);
	if(n > 2)
		buf[n - 2] = 0;
	
	text2.format(L"%s[Reason:%s]", text.operator const wchar_t *(), buf);

	m_message = wcsdup(text2);
}

JException::~JException(void)
{
	if(m_message)
	{
		free(m_message);
		m_message = NULL;
	}
}
